package exam;

import exam.bean.StuInfo;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

@WebServlet(name = "ResultServlet", value = "/ResultServlet")
public class ResultServlet extends HttpServlet {


    //读取结果
    protected List<StuInfo> readResult(){
        ObjectInputStream ois =null;
        FileInputStream fis = null;
        List<StuInfo> stuInfoList = new ArrayList<>();
        try {
            fis = new FileInputStream("../stuInfo.txt");
            ois = new ObjectInputStream(fis);
            while(fis.available()>0){
                StuInfo stu =(StuInfo) ois.readObject();
                stuInfoList.add(stu);
        }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        finally {
            try {
                if(fis != null){
                    fis.close();
                }
                if(ois != null){
                    ois.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return stuInfoList;
    }
    //展示结果
    protected void showResult(List<StuInfo> stuInfoList,HttpServletResponse response) throws IOException {
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();
        out.println("<html><head>");
        out.println("<title>得分情况</title>");
        out.println(" <link type=\"text/css\" rel=\"stylesheet\" href=\"css/style.css\">");
        out.println("</head><body>");
        out.println("<table>\n" +
                "    <tr>\n" +
                "        <td class=\"biaoti\">2022-2023Web应用测试成绩</td>\n" +
                "    </tr>\n" +
                "</table>\n" +
                "<table class=\"table_content\">\n" +
                "    <tr>\n" +
                "        <td class=\"table_title\">学号</td>\n" +
                "        <td class=\"table_title\">姓名</td>\n" +
                "        <td class=\"table_title\">成绩</td>\n" +
                "    </tr>\n");
        for (StuInfo stu : stuInfoList
        ) {
            out.println(" <tr>\n" +
                    "        <td class=\"table_row\">" + stu.getStuId() + "</td>\n" +
                    "        <td class=\"table_row\">" + stu.getStuName() + "</td>\n" +
                    "        <td class=\"table_row\">" + stu.getGrade() + "</td>\n" +
                    "    </tr>");
        }
        out.println("</table>");
        out.println("</body></html>");
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException {

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException {

        try {
            List<StuInfo> stuInfos = readResult();
            showResult(stuInfos,response);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    }
